using System;
using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class EndPVPWindow : BaseWindow
{
	public static EndPVPWindow instance;

	public GameObject anchorLeft;

	public GameObject anchorRight;

	public GameObject anchorCenter;

	public GameObject panelOverMatch;

	public UILabel labelWinCash;

	public UILabel labelKillPVP;

	public UILabel labelAvarege;

	public UILabel labelWinExp;

	public UILabel labelLoseExp;

	public UILabel labelMatchOverUWin;

	public UILabel labelMatchIsOver;

	public UILabel labelRespect;

	public GameObject panelRecord;

	public GameObject panelAverage;

	public UIPanel panelTable;

	public UIPanel panelAvarege;

	public UIPanel panelRespectWindow;

	public UIPanel panelScrollView;

	public UIPanel panelKillCount;

	public UIPanel headerFirst;

	public UIPanel headerSecond;

	public UIPanel headerThird;

	public UIPanel headerNormal;

	public EndPVPReputationProgress respectBar;

	public UISprite blackBackGround;

	public GameObject panelWin;

	public GameObject panelNoWin;

	private Vector3 scaleText = Vector3.one * 2f;

	private Animator moveLabelOverMatch;

	private Animator moveLabelYouWin;

	public GameObject killStreakGO;

	public GameObject playerPanel;

	private float timerForScaleText = 0.5f;

	public int money;

	public int average;

	public int experience;

	public EndPVPMapSelect endPVPMapSelect;

	private UIPanel header;

	private UIWidget left;

	private UIWidget right;

	public int moneyForCalc;

	public int averageForCalc;

	public int experienceForCalc;

	public float timeScale;

	private void Start()
	{
		HOTween.Init();
		moveLabelOverMatch = labelMatchIsOver.GetComponent<Animator>();
		moveLabelYouWin = labelMatchOverUWin.GetComponent<Animator>();
		left = anchorLeft.GetComponent<UIWidget>();
		right = anchorRight.GetComponent<UIWidget>();
		if (endPVPMapSelect == null)
		{
			endPVPMapSelect = GetComponentInChildren<EndPVPMapSelect>(true);
		}
	}

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void OnEnable()
	{
	}

	private void OnDisable()
	{
	}

	public override void OnPreShow(object userData)
	{
		if (GameController.instance.playerScript != null)
		{
			GameController.instance.playerScript.isDead = true;
		}
	}

	public override void OnShow(object userData)
	{
		anchorRight.SetActive(true);
		anchorLeft.SetActive(true);
		anchorCenter.SetActive(true);
	}

	public void ShowMainPVPMenu(ReputationMapInfo info, float firstRep)
	{
		instance.OnShow(null);
		KillStreakScrollView.init.CreateKillStreakImage();
		labelWinExp.text = "0";
		labelWinCash.text = "0";
		labelKillPVP.text = "0";
		left.alpha = 0f;
		right.alpha = 0f;
		panelTable.alpha = 0f;
		panelAvarege.alpha = 0f;
		panelScrollView.alpha = 0f;
		panelKillCount.alpha = 0f;
		respectBar.Init((int)firstRep, (int)info.maxReputation);
		StartCoroutine(ShowBarRespect());
		HOTween.To(blackBackGround, 0.5f, new TweenParms().Prop("alpha", 0f).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			labelMatchOverUWin.gameObject.SetActive(false);
			labelMatchIsOver.gameObject.SetActive(false);
			panelOverMatch.gameObject.SetActive(false);
			ShowFirstsLabel();
		}));
		headerNormal.gameObject.SetActive(false);
		headerFirst.gameObject.SetActive(false);
		headerSecond.gameObject.SetActive(false);
		headerThird.gameObject.SetActive(false);
		header = headerNormal;
		int num = PVPManager.instance.myPlayerPlace;
		if (PVPManager.instance.myPlayerPlace == 1 && !IsFirstPlace())
		{
			num = 2;
		}
		switch (num)
		{
		case 1:
			header = headerFirst;
			break;
		case 2:
			header = headerSecond;
			break;
		case 3:
			header = headerThird;
			break;
		}
	}

	private void FadeOut(UIRect rect, Action callBack)
	{
		rect.alpha = 0f;
		rect.gameObject.SetActive(true);
		HOTween.To(rect, 0.5f, new TweenParms().Prop("alpha", 1f).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			if (callBack != null)
			{
				callBack();
			}
		}));
	}

	private void ShowFirstsLabel()
	{
		FadeOut(header, null);
		FadeOut(panelAvarege, ScaleAllLabel);
		FadeOut(panelTable, null);
		FadeOut(panelScrollView, null);
		FadeOut(panelKillCount, null);
	}

	public override void OnHide()
	{
		anchorLeft.SetActive(false);
		anchorCenter.SetActive(false);
		anchorRight.SetActive(false);
	}

	public void exitToMenu()
	{
		Save.SaveBool(settings.keyLoadPVPMenu, true);
		KillStreakScrollView.init.ClearKillStreak();
		GameController.instance.exitToMenu();
	}

	public void StartAnimationYouWin()
	{
		moveLabelYouWin.speed = 2f;
		moveLabelYouWin.SetTrigger("MoveLabel");
	}

	public void StartAnimationMatchOver()
	{
		moveLabelOverMatch.speed = 2f;
		moveLabelOverMatch.SetTrigger("MoveLabel");
	}

	public void StopAnimation()
	{
	}

	public void RestartLevel()
	{
		KillStreakScrollView.init.ClearKillStreak();
		GameController.instance.RestartLevel();
	}

	private void ScaleAllLabel()
	{
		if (IsFirstPlace())
		{
			ScaleLabel(labelWinExp, new Color(1f, 1f, 0f, 1f));
			ScaleLabel(labelWinCash, new Color(0f, 1f, 0f, 1f));
			ScaleLabel(labelKillPVP, new Color(1f, 0f, 0f, 1f));
		}
		else
		{
			ScaleLabel(labelKillPVP, new Color(1f, 0f, 0f, 1f));
		}
		StartCoroutine(StartCalculation());
	}

	private IEnumerator StartCalculation()
	{
		yield return null;
		experience = 100;
		money = 100;
		CalculationLabelMoneyExpAvr(0.5f, 0.5f);
	}

	public void ShowStartAndMenu()
	{
		HOTween.To(left, 0.5f, new TweenParms().Prop("alpha", 1f));
		HOTween.To(right, 0.5f, new TweenParms().Prop("alpha", 1f));
		endPVPMapSelect.Show();
	}

	private void ScaleLabel(UILabel text, Color color)
	{
		HOTween.To(text, timerForScaleText, new TweenParms().Prop("color", color));
		HOTween.To(text.transform, timerForScaleText, new TweenParms().Prop("localScale", scaleText).OnComplete((TweenDelegate.TweenCallback)delegate
		{
		}));
	}

	public bool IsFirstPlace()
	{
		return PVPManager.instance.myPlayerPlace == 1 && GameController.instance.playerScript.points != 0 && GameController.instance.listPlayers.Count > 1 && GameController.instance.listPlayers[0].points != GameController.instance.listPlayers[1].points;
	}

	private void CalculationLabelMoneyExpAvr(float timeForCalc, float timeForScale)
	{
		if (IsFirstPlace())
		{
			moneyForCalc = 0;
			HOTween.To(labelWinCash.transform, timeForCalc, new TweenParms().Prop("localScale", new Vector3(3f, 3f, 3f)));
			HOTween.To(this, timeForCalc, new TweenParms().Prop("moneyForCalc", money).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				labelWinCash.text = string.Empty + moneyForCalc;
			}).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(labelWinCash.transform, timeForScale, new TweenParms().Prop("localScale", Vector3.one));
				HOTween.To(labelWinCash, timeForScale, new TweenParms().Prop("color", new Color(1f, 1f, 1f, 1f)));
			}));
			averageForCalc = 0;
			HOTween.To(labelKillPVP.transform, timeForCalc, new TweenParms().Prop("localScale", new Vector3(3f, 3f, 3f)));
			HOTween.To(this, timeForCalc, new TweenParms().Prop("averageForCalc", average).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				labelKillPVP.text = string.Empty + averageForCalc;
			}).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(labelKillPVP.transform, timeForScale, new TweenParms().Prop("localScale", Vector3.one));
				HOTween.To(labelKillPVP, timeForScale, new TweenParms().Prop("color", new Color(1f, 1f, 1f, 1f)));
			}));
			experienceForCalc = 0;
			HOTween.To(labelWinExp.transform, timeForCalc, new TweenParms().Prop("localScale", new Vector3(3f, 3f, 3f)));
			HOTween.To(this, timeForCalc, new TweenParms().Prop("experienceForCalc", money).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				labelWinExp.text = string.Empty + experienceForCalc;
			}).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(labelWinExp.transform, timeForScale, new TweenParms().Prop("localScale", Vector3.one));
				HOTween.To(labelWinExp, timeForScale, new TweenParms().Prop("color", new Color(1f, 1f, 1f, 1f)).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					StartCoroutine(KillStreakScrollView.init.ShowAllEvents());
				}));
			}));
		}
		else
		{
			averageForCalc = 0;
			HOTween.To(labelKillPVP.transform, timeForCalc, new TweenParms().Prop("localScale", new Vector3(3f, 3f, 3f)));
			HOTween.To(this, timeForCalc, new TweenParms().Prop("averageForCalc", average).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
				labelKillPVP.text = string.Empty + averageForCalc;
			}).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(labelKillPVP.transform, timeForScale, new TweenParms().Prop("localScale", Vector3.one));
				HOTween.To(labelKillPVP, timeForScale, new TweenParms().Prop("color", new Color(1f, 1f, 1f, 1f)));
			}));
			experienceForCalc = 0;
			HOTween.To(this, timeForCalc, new TweenParms().Prop("experienceForCalc", experience).OnUpdate((TweenDelegate.TweenCallback)delegate
			{
			}).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				StartCoroutine(KillStreakScrollView.init.ShowAllEvents());
			}));
		}
	}

	public void SlowMotion(bool place)
	{
		if (instance.gameObject.GetActive())
		{
			return;
		}
		MarketController.Instance.HideMarket();
		WindowManager.instance.ShowWindow(typeof(EndPVPWindow), true);
		UISprite sprite = blackBackGround;
		MessageWindow.instance.OnExitMapTriggerEventHandler();
		UILabel label;
		if (place)
		{
			label = labelMatchOverUWin;
			panelWin.SetActive(true);
			panelNoWin.SetActive(false);
		}
		else
		{
			label = labelMatchIsOver;
			panelWin.SetActive(false);
			panelNoWin.SetActive(true);
		}
		label.transform.localScale = Vector3.one * 1.5f;
		label.transform.localPosition = Vector3.zero;
		panelOverMatch.SetActive(true);
		sprite.gameObject.SetActive(true);
		label.gameObject.SetActive(true);
		timeScale = 0f;
		OnHide();
		HOTween.To(this, 0.7f, new TweenParms().Prop("timeScale", 1f).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			Time.timeScale = Mathf.Lerp(0.7f, 0.5f, timeScale);
		}));
		label.alpha = 0f;
		sprite.alpha = 0f;
		HOTween.To(label, 0.7f, new TweenParms().Prop("alpha", 1f).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			if (place)
			{
				StartAnimationYouWin();
			}
			else
			{
				StartAnimationMatchOver();
			}
			label.transform.localScale = Vector3.one;
			HOTween.To(sprite, 0.5f, new TweenParms().Prop("alpha", 1f));
			StartCoroutine(StartAfterSlowMotion());
		}));
	}

	private IEnumerator StartAfterSlowMotion()
	{
		yield return new WaitForSeconds(0.8f);
		Time.timeScale = 1f;
		PVPManager.instance.EndPVPGame();
	}

	private void ScaleCalculationLabel(UILabel text, int count, int startCount = 0)
	{
		if (startCount <= count)
		{
			text.text = string.Empty + startCount;
			HOTween.To(text.transform, 0.2f, new TweenParms().Prop("localScale", scaleText * 1.5f).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(text.transform, 0.2f, new TweenParms().Prop("localScale", scaleText).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					ScaleCalculationLabel(text, count, startCount + 5);
				}));
			}));
		}
		else
		{
			HOTween.To(text.transform, timerForScaleText, new TweenParms().Prop("localScale", Vector3.one));
			HOTween.To(text, timerForScaleText, new TweenParms().Prop("color", new Color(1f, 1f, 1f, 1f)));
		}
	}

	public IEnumerator ShowBarRespect()
	{
		yield return new WaitForSeconds(0.2f);
		respectBar.AddFillAmount(PVPManager.instance.addReputation, delegate
		{
			if (PVPManager.instance.myRespect != PVPManager.instance.maxReputationFromThisMap)
			{
			}
		});
	}

	public void GetReward()
	{
		panelRespectWindow.gameObject.SetActive(false);
	}
}
